from math import factorial
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

def bezier():
    bezier_data = pd.read_csv('Type_Bezier.txt', delim_whitespace=True, header=None)
    bezier_data.columns = ['p1_lat', 'p1_lon', 'p2_lat', 'p2_lon', 'p3_lat', 'p3_lon', 'p4_lat', 'p4_lon', 'channel_type']
    p1_lat = bezier_data['p1_lat']
    p1_lon = bezier_data['p1_lon']
    p2_lat = bezier_data['p2_lat']
    p2_lon = bezier_data['p2_lon']
    p3_lat = bezier_data['p3_lat']
    p3_lon = bezier_data['p3_lon']
    p4_lat = bezier_data['p4_lat']
    p4_lon = bezier_data['p4_lon']

    # points = [[p1_lat,p1_lon],  [p2_lat, p2_lon], [p4_lat, p4_lon], [p3_lat, p3_lon]]# 在此处修改坐标
    # points = [[p1_lat, p1_lon], [p2_lat, p2_lon], [p4_lat, p4_lon], [p3_lat, p3_lon]]  # 在此处修改坐标
    # points = [[p1_lat, p1_lon], [p3_lat, p3_lon], [p2_lat, p2_lon], [p3_lat, p3_lon]]  # 在此处修改坐标
    # points = [[p1_lat, p1_lon], [p2_lat, p2_lon], [p3_lat, p3_lon], [p1_lat, p1_lon]]  # 在此处修改坐标
    # points = [[p1_lat, p1_lon], [p2_lat, p2_lon], [p4_lat, p4_lon], [p3_lat, p3_lon]]  # 在此处修改坐标
    points = [[p3_lat, p3_lon], [p1_lat, p1_lon], [p2_lat, p2_lon], [p4_lat, p4_lon]]  # 在此处修改坐标
    # points_con = [[p4_lat, p4_lon],[p2_lat, p2_lon], [p1_lat,p1_lon], [p3_lat, p3_lon]]  # 在此处修改坐标



    N = len(points)
    n = N - 1
    px = []
    py = []
    for T in range(1001):
        t = T*0.001
        x,y = 0,0
        for i in range(N):
            B = factorial(n)*t**i*(1-t)**(n-i)/(factorial(i)*factorial(n-i))
            x += points[i][0]*B
            y += points[i][1]*B
        px.append(x)
        py.append(y)
    return px,py,points,bezier_data
if __name__ == '__main__':
    px,py,points,bezier_data = bezier()
    plt.plot(px,py,color='green')
    # plt.plot([i[0] for i in points],[i[1] for i in points],'r.')
    plt.show()
